<?php

namespace App\Admin\Controllers;

use App\Coupon;
use App\CouponSetting;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\Carbon;

class CouponSettingController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('优惠券发放计划列表');
            $content->description('优惠券发放计划列表');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('编辑优惠券计划');
            $content->description('编辑优惠券计划');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('增加优惠券计划');
            $content->description('增加优惠券计划');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(CouponSetting::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->name('计划名称');
            $grid->type('发放类型')->display(function($type){
                return $type==0 ? '关注发送' : '满足条件发送';
            });
            $grid->column('coupon.name','优惠券名称');
            $grid->order_num('满足推广订单数');
            $grid->money('满足订单金额');
            $grid->user_num('满足会员数');
            $grid->start_time('有效开始时间');
            $grid->end_time('有效结束时间');
            $grid->disableExport();
            $grid->disableFilter();
        });
    }
    public function coupon(){
        return Coupon::select('id','name as text')->where('end_time','>',Carbon::now())->get();
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(CouponSetting::class, function (Form $form) {

            $form->display('id', 'ID');
            $form->text('name','计划名称')->rules('required',['required'=>'计划名称不能为空']);
            $form->select('type','计划类型')->options(['0'=>'关注公众号发送','1'=>'满足条件发送'])->default(0);
            $form->select('coupon_id','优惠券')->options(Coupon::getSelectOptions());
            $form->number('order_num','满足的推广订单数')->default(0)->help('当计划类型为关注公众号发送时,此设置项无效');
            $form->number('user_num','满足的推广会员数')->default(0)->help('当计划类型为关注公众号发送时,此设置项无效');
            $form->currency('money','满足的推广金额数')->symbol('元')->default(0)->help('当计划类型为关注公众号发送时,此设置项无效');
            $form->datetimeRange('start_time','end_time','有效时间段')->rules('required',['required'=>'有效时间段必填']);
        });

    }
}
